package com.tongan.entity.button;

import com.tongan.entity.config.CgFormFieldEntity;
import com.tongan.entity.config.CgFormHeadEntity;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**   
 * @Title: Entity
 * @Description: 表单自定义按钮
 * @author 张代浩
 * @date 2013-08-07 20:16:26
 * @version V1.0   
 *
 */
@Entity
@Table(name = "cgform_button", schema = "")
@SuppressWarnings("serial")
@Data
public class CgformButtonEntity implements java.io.Serializable {
	/**id*/
	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name ="ID",nullable=false,length=32)
	private String id;
	/**外键关联cgform_head*/
	@Column(name ="FORM_ID",nullable=true,length=32)
	private String formId;
	/**按钮编码*/
	@Column(name ="BUTTON_CODE",nullable=true,length=50)
	private String buttonCode;
	public String getetButtonCode(){
		return StringUtils.capitalize(buttonCode);
	}
	public String getvrButtonCode(){
		return StringUtils.uncapitalize(buttonCode);
	}
	/**按钮名称*/
	@Column(name ="BUTTON_NAME",nullable=true,length=50)
	private String buttonName;
	/**按钮样式link/button*/
	@Column(name ="BUTTON_STYLE",nullable=true,length=20)
	private String buttonStyle;
	/**动作类型:js/bus*/
	@Column(name ="OPT_TYPE",nullable=true,length=20)
	private String optType;
	/**显示表达式:exp="status#eq#0"*/
	@Column(name ="EXP",nullable=true,length=255)
	private String exp;
	/**0:禁用/1:使用*/
	@Column(name ="BUTTON_STATUS",nullable=true,length=2)
	private String buttonStatus;
	/**顺序*/
	@Column(name ="order_num",nullable=true,length=4)
	private Integer orderNum;
	/**按钮图标样式*/
	@Column(name ="button_icon",nullable=true,length=20)
	private String buttonIcon;

	/**
	 * 表单处理形式
	 * void 表示不提交表单
	 * ajax 表示处理完后 用ajax返回
	 */
	String formProcessType;//void ajax

	///==============================
	/**
	 * newadd 功能类型
	 * BasicType 普通表单
	 * ManyToManyType 多对多类型
	 */
	@Enumerated(EnumType.STRING)
	private CButtonFunctionTypeEnum functionType;


	@ManyToOne
	@JoinColumn(name = "manytomanytableid")
	private CgFormHeadEntity manytomanytable;

	@Transient
	private List<CgformButtonFieldConfig> fieldConfigs=new ArrayList<>();

	/**
	 * [模版引用]列配置map
	 * fieldId为key
	 */
	@Transient
	private Map<String,Object> fieldConfigsMap=new HashMap<>();


	//=================================

	/**
	 * 获取当前表在多对多表中对应的ID字段
	 * @param table
	 * @return
	 */
	public CgFormFieldEntity getCurrentField(CgFormHeadEntity table){
		if (manytomanytable!=null){
			for (CgFormFieldEntity cgFormFieldEntity:manytomanytable.getManyToManyFields()) {
				if (cgFormFieldEntity.getForeignTableId().equalsIgnoreCase(table.getId())){
					return cgFormFieldEntity;
				}
			}
		}
		return null;
	}

	/**
	 * 获取当前表在多对多表中对应的ID字段的反字段
	 * @param table
	 * @return
	 */
	public CgFormFieldEntity getCurrentOppositeField(CgFormHeadEntity table){
		if (manytomanytable!=null){
			for (CgFormFieldEntity cgFormFieldEntity:manytomanytable.getManyToManyFields()) {
				if (cgFormFieldEntity.getForeignTableId().equalsIgnoreCase(table.getId())){
					return cgFormFieldEntity.getOppositeField();
				}
			}
		}
		return null;
	}

	/**
	 * 获取当前表在多对多表中对应的ID字段的反字段的外键字段
	 * @param table
	 * @return
	 */
	public CgFormFieldEntity getCurrentOppositeForeignField(CgFormHeadEntity table){
		return getCurrentOppositeField(table).getForeignFieldEntity();
	};

}

